const express = require('express');
const router = express.Router();
const path = require('path');
const logger = require('../utils/logger');

// 导入路由模块
const chatRoutes = require('./chat');
const weatherRoutes = require('./weather');
const familyRoutes = require('./family');
const asrRoutes = require('./asr');

// API路由
router.use('/api/chat', chatRoutes);
router.use('/api/weather', weatherRoutes);
router.use('/api/family', familyRoutes);
router.use('/api/asr', asrRoutes);

// 静态文件服务
router.use('/uploads', express.static(path.join(__dirname, '../uploads')));

// API根路由
router.get('/api', (req, res) => {
  res.json({
    success: true,
    message: 'Ctrl100Panel API 服务正常运行',
    version: '1.0.0',
    endpoints: [
      '/api/chat',
      '/api/weather',
      '/api/family',
      '/api/asr'
    ]
  });
});

// 健康检查路由
router.get('/health', (req, res) => {
  res.json({
    status: 'ok',
    uptime: process.uptime(),
    timestamp: new Date().toISOString()
  });
});

// 404处理
router.use('/api/*', (req, res) => {
  logger.warn(`未找到API路由: ${req.originalUrl}`);
  res.status(404).json({
    success: false,
    message: '未找到请求的API路由'
  });
});

// 前端路由处理（SPA支持）
router.get('*', (req, res) => {
  // 将所有非API请求转发到前端应用
  res.sendFile(path.join(__dirname, '../../frontend/build/index.html'));
});

module.exports = router;